<?php
/**
*+-------订阅中心操作类------------------------+
*+-------------------------------------------------+
*+-------------用户订阅游戏操作--------------------+
*+-------------------------------------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Home\Controller;

class ScribeController extends BaseUserController {

	/**
	*错误操作信息
	*/
	protected $errInfo = '';

	protected function _initialize(){
		parent::_initialize();
	}


	/**
	*用户订阅
	*/
	public function doScribe(){

		$data = I('');
		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		$eventObj = A('Api/GameScribe', 'Event');

		$data['game'] = intval($data['game']);
		$res = $eventObj->scribe(UID, $data['game']);
		if(!$res){

			$this->ajaxReturn(array('state'=>-2, 'info'=>'已经订阅'));

		}else{

			$game = A('Api/Game', 'Event')->info($data['game']);
			$total = intval($game['scribe']) + 1;

			$this->ajaxReturn(array('state'=>0, 'info'=>'订阅成功', 'data'=>array('total'=>$total)));
		}

	}

	/**
	*用户取消订阅
	*/
	public function unScribe(){

		$data = I('');
		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		$eventObj = A('Api/GameScribe', 'Event');

		$data['game'] = intval($data['game']);
		$res = $eventObj->unscribe(UID, $data['game']);
		if(!$res){

			$this->ajaxReturn(array('state'=>-2, 'info'=>'未订阅不能取消'));

		}else{

			$game = A('Api/Game', 'Event')->info($data['game']);
			$total = intval($game['scribe']) - 1;

			$this->ajaxReturn(array('state'=>0, 'info'=>'取消成功', 'data'=>array('total'=>$total)));
		}

	}

	/**
	*订阅参数的检查
	*/
	private function checkParam($data){

		if(empty($data['game'])){

			$this->errInfo = '参数有误';
			return false;
		}

		$game = A('Api/Game', 'Event')->info($data['game']);

		if (!$game){

			$this->errInfo = '游戏信息有误';
			return false;
		}

		return true;
	}
}
